<template>
  <div>
    <div style="text-align: right">
      <el-button @click="toAdd()">新增</el-button>
    </div>
    <el-table
        :data="tableData"
        style="width: 100%">
      <el-table-column
          label="角色名称"
          width="120">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column
          label="角色标签"
          width="120">
        <template slot-scope="scope">
            <div slot="reference" class="name-wrapper">
              <el-tag v-if="scope.row.tags != undefined && scope.row.tags != ''" size="medium">{{ scope.row.tags }}</el-tag>
            </div>
        </template>
      </el-table-column>
      <el-table-column
          label="角色描述"
          width="260">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.description }}</span>
        </template>
      </el-table-column>
      <el-table-column
          label="创建时间"
          width="180">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.pageNo"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="listQuery.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>

export default {
  name: "RoleManage",
  data() {
    return {
      listQuery: {
        "pageNo": 1,
        "pageSize": 10
      },
      total: 50,
      tableData: []
    }
  },
  methods: {
    async getList() {
      let res = await this.$http.post('/api/role/list', this.listQuery)
      this.total = res.data.total
      this.tableData = res.data.data
    },
    handleEdit(index, row) {
      this.$router.push('/roleEdit/'+row.id)
    },
    handleDelete(index, row) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.delete('/api/role/deleteById?id='+row.id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
        this.getList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.pageNo = val
      this.getList()
    },
    toAdd() {
      this.$router.push('/roleAdd')
    }
  },
  created() {
    this.getList()
  },
  activated() {
    this.getList()
  },
  mounted() {
    this.$bus.$on('page-refresh', name => {
      if (name === 'RoleManage') {
        this.getList()
      }
    })
  }
}
</script>

<style lang="scss" scoped>
.block {
  text-align: right;
  margin-top: 10px;
}
</style>
